﻿using System;
using System.Text.RegularExpressions;

namespace Hoguet.RTM
{
    public class RtmTimeEstimator : IRtmTimeEstimator
    {
        private readonly double _defaultHours;

        public RtmTimeEstimator(double defaultHours)
        {
            _defaultHours = defaultHours;
            RtmMinuteRegex = new Regex(@"^(\d+)\s*m", RegexOptions.IgnoreCase);
            RtmHourRegex = new Regex(@"^(\d+)\s*h", RegexOptions.IgnoreCase);
        }
        public Regex RtmMinuteRegex { get; set; }
        public Regex RtmHourRegex { get; set; }
        
        public double GetEstimateInHours(string estimate)
        {
            if (String.IsNullOrEmpty(estimate))
            {
                return _defaultHours;
            }
            if (RtmHourRegex.IsMatch(estimate))
            {
                return Convert.ToInt32(RtmHourRegex.Matches(estimate)[0].Groups[1].Value);
            }
            if (RtmMinuteRegex.IsMatch(estimate))
            {
                return Convert.ToInt32(RtmMinuteRegex.Matches(estimate)[0].Groups[1].Value) / 60.0;
            }
            return _defaultHours;
        }
    }
}